﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Basic_sprite_code
{
    class Hero:Sprite
    {
        public Apprentice target;

        public int health;
        public float speed;
        public Vector2 flyVel;
        public int animCounter;

        public bool fireDeath;
        public bool goblinDeath;
        public bool windDeath;
        public bool dragonDeath;
        public bool enemyOnLeft;

        public Texture2D pic1;
        public Texture2D pic2;
        public Texture2D burntPic;

        public Hero(DarkLordsApprentice g, Vector2 position, Apprentice t, int hp, float spd)
            : base(g, "HeroWalking1", position, new Vector2(50, 50))
        {
            target = t;

            health = hp;
            speed = spd;
            flyVel = new Vector2(0,0);
            animCounter = 0;

            fireDeath = false;
            goblinDeath = false;
            windDeath = false;
            dragonDeath = false;
            enemyOnLeft = false;

            pic1 = game.Content.Load<Texture2D>("HeroWalking1");
            pic2 = game.Content.Load<Texture2D>("HeroWalking2");
            burntPic = game.Content.Load<Texture2D>("HeroBurnt");

            scale = 0.05f;

        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            Random rand = new Random();

            if (!game.gamePaused)
            {
                foreach (Sprite s in AllSprites)
                {
                    if (s is Apprentice && !fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                    {
                        Apprentice a = (Apprentice)s;
                        Vector2 aOffset = this.position - a.position;

                        if (aOffset.Length() < 20)
                        {
                            target.helper.playingGame = false;
                            foreach (Sprite s2 in AllSprites)
                            {
                                if (!(s2 is StoryHelper))
                                {
                                    s2.position.X = 1000;
                                    s2.position.Y = 1000;
                                }


                            }
                        }
                    }

                    if (s is Fire && !fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                    {
                        Fire f = (Fire)s;
                        Vector2 offset = this.position - f.position;
                        if (offset.Length() < (this.scale * 50 + f.scale * 50) / 2)
                        {
                            if (health > 0)
                            {
                                health -= f.firePower;
                            }
                            else if (!fireDeath)
                            {
                                fireDeath = true;
                                animCounter = 0;
                                target.exp++;
                            }

                        }
                    }

                    if (s is Goblin && !fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                    {
                        Goblin gob = (Goblin)s;
                        Vector2 gobOffset = this.position - gob.position;
                        if (gobOffset.Length() < 50 && !gob.slain)
                        {
                            if (this.position.X - gob.position.X > 0)
                            {
                                enemyOnLeft = true;
                                gob.position.X += 50;
                                flyVel.X = -10;
                                flyVel.Y = -20;
                            }
                            else
                            {
                                enemyOnLeft = false;
                                gob.position.X -= 50;
                                flyVel.X = -10;
                                flyVel.Y = -20;
                            }

                            if (rand.NextDouble() < gob.deathChance)
                            {
                                gob.slain = true;
                                gob.animCounter = 0;
                                if (gob.guard)
                                {
                                    target.circle.summonPresent = false;
                                    target.circle.magicAbsorbed = 0;
                                }
                            }

                            goblinDeath = true;
                            target.exp++;
                        }
                    }

                    if (s is WindDemon && !fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                    {
                        WindDemon wd = (WindDemon)s;
                        Vector2 wdOffset = wd.position - this.position;
                        if (wdOffset.Length() < 80)
                        {
                            if (!wd.attacking || !wd.slain)
                            {
                                wd.animCounter = 0;
                                wd.attacking = true;
                                if (rand.NextDouble() < wd.deathChance)
                                {
                                    wd.slain = true;
                                    wd.animCounter = 0;
                                    if (wd.guard)
                                    {
                                        target.circle.summonPresent = false;
                                        target.circle.magicAbsorbed = 0;
                                    }
                                }
                            }
                            windDeath = true;
                            target.exp++;
                            flyVel.X = wdOffset.X;
                        }
                    }

                    if (s is Dragon && !fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                    {
                        Dragon d = (Dragon)s;
                        Vector2 dOffset = d.position - this.position;
                        float dist = dOffset.Length();
                        if (dOffset.Length() < (200 * d.scale) && !d.slain)
                        {
                            if (!d.attacking && dOffset.Length() < 60)
                            {
                                d.animCounter = 0;
                                d.attacking = true;
                                if (rand.NextDouble() < d.deathChance)
                                {
                                    d.slain = true;
                                    d.animCounter = 0;
                                    if (d.guard)
                                    {
                                        target.circle.summonPresent = false;
                                        target.circle.magicAbsorbed = 0;
                                    }
                                }
                            }
                            else if (dOffset.Length() <= 80)
                            {
                                dragonDeath = true;
                                target.exp++;
                                flyVel.X = -10;
                                flyVel.Y = -20;
                            }

                        }
                    }


                }

                float posScale = (position.Y - 150.0f) / 300.0f;

                if (!fireDeath && !goblinDeath && !windDeath && !dragonDeath)
                {
                    switch (animCounter)
                    {
                        case 0:
                            image = pic1;
                            animCounter++;
                            break;

                        case 20:
                            image = pic2;
                            animCounter++;
                            break;

                        case 40:
                            animCounter = 0;
                            break;

                        default:
                            animCounter++;
                            break;
                    }

                    if (position.Y + 50 < 450)
                    {
                        position.Y += speed;// *posScale;
                        scale = 0.25f + posScale * 0.75f;
                    }
                    else if (position.Y > 540 && position.Y <= 545)
                    {
                        position.X += speed;
                    }
                    else if (position.Y > 545)
                    {
                        position.Y -= speed;
                    }
                    else
                    {
                        position.X = 150.0f;
                        position.Y = 800.0f;
                        pic1 = game.Content.Load<Texture2D>("HeroSide1");
                        pic2 = game.Content.Load<Texture2D>("HeroSide2");
                        burntPic = game.Content.Load<Texture2D>("HeroBurntSide");
                        scale = 1;
                    }
                }
                else if (fireDeath)
                {
                    switch (animCounter)
                    {
                        case 0:
                            image = burntPic;
                            animCounter++;
                            break;

                        case 60:
                            image = game.Content.Load<Texture2D>("HeroAsh");
                            animCounter++;
                            break;

                        case 120:
                            game.Components.Remove(this);
                            break;

                        default:
                            animCounter++;
                            break;
                    }
                }
                else if (goblinDeath || dragonDeath)
                {
                    if (position.X < -100 || position.X > 900 || position.Y > 600 || position.Y < -100)
                    {
                        game.Components.Remove(this);
                    }
                    else
                    {
                        position.X += flyVel.X;
                        position.Y += flyVel.Y;
                        flyVel.Y += 1;
                        orientation += 5;
                    }
                }
                else if (windDeath)
                {
                    if (position.Y < -100)
                    {
                        game.Components.Remove(this);
                    }
                    else
                    {
                        position.X += flyVel.X;
                        flyVel.Y += 1;
                        position.Y += flyVel.Y;
                    }
                }

                base.Update(gameTime);
            }
        }

    }
}
